import "dart:developer";

import "package:dio/dio.dart";
import "package:sports_new/entity/expert/subs_article_list_entity.dart";
import "package:sports_new/entity/home/news_comment_entity.dart";
import "package:sports_new/entity/live/live_detail_entity.dart";
import "package:sports_new/entity/home/news_class2_entity.dart";
import "package:sports_new/entity/home/news_class_entity.dart";
import "package:sports_new/entity/home/news_item_entity.dart";
import "package:sports_new/entity/home/news_match_entity.dart";
import "package:sports_new/entity/home/user_detail_entity.dart";
import "package:sports_new/entity/match/expert_views_entity.dart";
import "package:sports_new/entity/other/resource_entity.dart";
import "package:sports_new/server/http/dio_utils.dart";
import "package:sports_new/utils/utils.dart";

import "../api_dir.dart";

class HomeApi {
  static Future<List<ResourceEntity>> getResources(String id) async {
    try {
      final r =
          await DioUtils.post("/resource/app-do/list", params: {"id": id});
      final l = r.data["d"] as List;
      return l.map((e) => ResourceEntity.fromJson(e)).toList();
    } catch (err) {}
    return [];
  }

  static Future<List<NewsMatchEntity>> getNewsMatchs() async {
    try {
      final r = await DioUtils.post("/${ApiDir.match}/app-fb-do/hotList");
      final l = r.data["d"] as List;
      return l.map((e) => NewsMatchEntity.fromJson(e)).toList();
    } catch (err) {}
    return [];
  }

  @Deprecated("使用2")
  static Future<List<NewsClassEntity>> getNewsClasses() async {
    try {
      final r = await DioUtils.post("/${ApiDir.info}/app-do/class/list");
      final l = r.data["d"] as List;
      return l.map((e) => NewsClassEntity.fromJson(e)).toList();
    } catch (err) {}
    return [];
  }

  static Future<List<NewsClass2Entity>> getNewsClasses2(bool all) async {
    try {
      final r = await DioUtils.post("/resource/app-do/classList",
          params: {"all": all, "categoryId": 6});
      final l = r.data["d"] as List;
      log("class list data = $l");
      return l.map((e) => NewsClass2Entity.fromJson(e)).toList();
    } catch (err) {}
    return [];
  }

  static Future<int?> updateNewsClasses(List<String> classIds) async {
    try {
      final result = await DioUtils.post("/resource/app-do/classListUpdate",
          params: {"classIds": classIds, "categoryId": 6});
      if (result.data != null) {
        return result.data["c"];
      }
    } catch (err) {}
    return null;
  }

  static Future<List<NewsItemEntity>> getNewsList(int id,
    {int page = 1, int size = 20}) async {
    final result = await DioUtils.post("/${ApiDir.info}/app-do/list", params: {
      "data": {"id": id},
      "page": page,
      "pageSize": size
    });
    if (result.statusCode == 200 && result.data["c"] == 200) {
      try {
        final List<NewsItemEntity> data = result.data["d"]["rows"].map<NewsItemEntity>(
          (e) => NewsItemEntity.fromJson(e)).toList();
        return data;
      }catch (e) {
        log("$e");
        return [];
      }
    }
    return [];
  }

  static Future<NewsItemEntity> getNews2(int id, {int? classId}) async {
    final result = await DioUtils.post("/${ApiDir.info}/app-do/news/info",
        params: {"id": id, "classId": classId});
    if (result.statusCode == 200 && result.data["c"] == 200) {
      final NewsItemEntity data = NewsItemEntity.fromJson(result.data["d"]);
      return data;
    }
    return NewsItemEntity();
  }

  //咨询 1:已读  2:点赞  3:转发  4取消点赞
  static Future<int?> newsSupport(int id,
      {required int type, String? comment}) async {
    final result = await DioUtils.post(
        "/${ApiDir.info}/info-news-comment-do/news/support",
        params: {"newsId": id, "type": type, "comment": comment});
    if (result.data != null) {
      return result.data["c"];
    }
    return null;
  }

  //评论 1.点赞 2.举报 3.取消点赞
  static Future<int?> support(int id,
      {required int type, required int newsId, String? comment}) async {
    final result = await DioUtils.post("/${ApiDir.info}/info-news-comment-do/support",
        params: {"id": id, "newsId": newsId, "comment": comment, "type": type});
    if (result.data != null) {
      return result.data["c"];
    }
    return null;
  }

  //评论列表
  static Future<Response> commentList(int newsId,
          {int? originId, int? type, int page = 1, int size = 20}) =>
      DioUtils.post("/${ApiDir.info}/info-news-comment-do/list", params: {
        "data": {"newsId": newsId, "originId": originId, "type": type},
        "page": page,
        "pageSize": size
      });

  //评论列表
  static Future<List<NewsCommentEntity>> getCommentList(int newsId,
      {int? originId, int? type, int page = 1, int size = 20}) async{
    final result = await DioUtils.post(
      "/${ApiDir.info}/info-news-comment-do/list",
      params: {
        "data": {"newsId": newsId, "originId": originId, "type": type},
        "page": page,
        "pageSize": size
      }
    );
    try {
      if (result.statusCode == 200 && result.data["c"] == 200) {
        List<NewsCommentEntity> data = result.data["d"]["rows"].map<NewsCommentEntity>(
          (e) => NewsCommentEntity.fromJson(e)
        ).toList();
        return data;
      }
    }catch(e){
      return [];
    }
    return [];
  }

  //删除评论
  static Future<Response> delete(int id) async =>
      DioUtils.post("/${ApiDir.info}/info-news-comment-do/delete", params: {"id": id});

  //删除评论
  static Future<int?> deleteComment(int id) async {
    final result = await DioUtils.post(
        "/${ApiDir.info}/info-news-comment-do/delete", params: {"id": id}
    );
    if (result.data != null) {
      return result.data["c"];
    }
    return null;
  }

  //评论 回复
  static Future<Response> add(int newsId, String comment,
          {int? fromId, int? originId}) =>
      DioUtils.post("/${ApiDir.info}/info-news-comment-do/add", params: {
        "comment": comment,
        "newsId": newsId,
        "fromId": fromId,
        "originId": originId
      });

  //用户主页
  static Future<UserDetailEntity?> getUserDetail(String id, int type) async {
    final result = await DioUtils.post("/${ApiDir.user}/app-user-home/userInfo",
        params: {"id": id, "type": type});
    if (result.statusCode == 200 && result.data["c"] == 200) {
      UserDetailEntity data = UserDetailEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  //用户全部发布
  static Future<List<dynamic>?> getUserAllPublish(int page, int pageSize,
      {String? userId, String? expertId}) async {
    final params = {
      "data": {"userId": userId, "expertId": expertId},
      "page": page,
      "pageSize": pageSize
    };
    final result =
        await DioUtils.post("/${ApiDir.user}/app-user-home/array", params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List list = [];
      for (dynamic data in result.data["d"]) {
        if (data["type"] == 1) {
          final d = NewsItemEntity.fromJson(data["data"]);
          list.add(d);
        } else if (data["type"] == 2) {
          final d = PlanEntity.fromJson(data["data"]);
          list.add(d);
        } else if (data["type"] == 3) {
          final d = ArticleDetail.fromJson(data["data"]);
          list.add(d);
        } else{
          list.add(null);
        }
      }
      return list;
    }
    return null;
  }

  //用户发布的文章视频帖子
  static Future<List<NewsItemEntity>?> getUserPublish(
      String userId, int type, int page, int pageSize) async {
    final params = {
      "data": {"userId": userId, "type": type},
      "page": page,
      "pageSize": pageSize
    };
    final result = await DioUtils.post("/${ApiDir.info}/app-publish-do/listWithUserHome",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<NewsItemEntity> data = result.data["d"]
          .map<NewsItemEntity>((e) => NewsItemEntity.fromJson(e))
          .toList();
      return data;
    }
    return null;
  }
}
